package com.dongdongshop.service.impl;

import com.dongdongshop.pojo.TbItem;
import com.dongdongshop.pojo.TbOrderItem;
import com.dongdongshop.service.CartVoSerivce;
import com.dongdongshop.service.TbItemCatService;
import com.dongdongshop.vo.CartVo;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

@Service
@DubboService
public class CartVoServiceImpl implements CartVoSerivce {

    @DubboReference
    private TbItemCatService tbItemCatService;
    @Autowired
    private RedisTemplate redisTemplate;

    //添加购物车
    @Override
    public List<CartVo> addaddcartList(List<CartVo> cartList, Long itemId, Integer num) {
        //根据商品Id查询商品数据
        TbItem tbItem = tbItemCatService.selectItemById(itemId);
        //根据查出来的数据获取他的商家Id
        String sellerId = tbItem.getSellerId();
        //判断这个商品的商家是否存在原来的购物车中
        CartVo c = null;
        for (CartVo cart : cartList) {
            if (Objects.equals(cart.getSellerName(), sellerId)) {
                c = cart;
                break;
            }
        }
        //如果不在,我们就在原来的购物车添加数据就行了
        if (c == null) {
            c = new CartVo();
            c.setSellerName(sellerId);
            List<TbOrderItem> orderItems = new ArrayList<>();
            TbOrderItem orderItem = new TbOrderItem();
            orderItem.setNum(num);
            orderItem.setSellerId(sellerId);
            orderItem.setTitle(tbItem.getTitle());
            orderItem.setPrice(tbItem.getPrice());
            orderItem.setTotalFee(new BigDecimal(num * tbItem.getPrice().doubleValue()));
            orderItem.setPicPath(tbItem.getImage());
            orderItem.setItemId(itemId);
            orderItem.setGoodsId(tbItem.getGoodsId());
            orderItems.add(orderItem);
            c.setCartDatailList(orderItems);
            cartList.add(c);
        } else {
            //如果存在
            //获取该商家的明细列表
            List<TbOrderItem> cartDatailList = c.getCartDatailList();
            //根据商家id判断该商家之前有没有将该商品放入明细列表中
            TbOrderItem orderItem = getorderItem(cartDatailList, itemId);
            if (orderItem == null) {
                //虽然存在该商家,但是不存在该商家商品,只需要在该商家明细中追加一条即可
                orderItem = new TbOrderItem();
                orderItem.setNum(num);
                orderItem.setSellerId(sellerId);
                orderItem.setTitle(tbItem.getTitle());
                orderItem.setPrice(tbItem.getPrice());
                orderItem.setTotalFee(new BigDecimal(num * tbItem.getPrice().doubleValue()));
                orderItem.setPicPath(tbItem.getImage());
                orderItem.setItemId(itemId);
                orderItem.setGoodsId(tbItem.getGoodsId());
                cartDatailList.add(orderItem);
                c.setCartDatailList(cartDatailList);
//                cartList.add(c);
            } else {
                //即存在该商家,有买过此商品
                orderItem.setNum(orderItem.getNum() + num);
                orderItem.setTotalFee(new BigDecimal(tbItem.getPrice().doubleValue() * orderItem.getNum()));
                if (orderItem.getNum() <= 0) {
                    //如果购入车中商品数量为0或者小于o删除该商品
                    cartDatailList.remove(orderItem);
                }
                if (cartDatailList.size() == 0) {
                    //如果一个商家下所有的明细都删除,那么这个商家页面要移除
                    cartList.remove(c);
                }
            }
        }
        return cartList;
    }

    //将购物车数据保存到redis中
    @Override
    public void addcartListredis(Long id, List<CartVo> data) {
        redisTemplate.boundHashOps("cartList").put(id, data);
    }

    //获取购物车数据到redis
    @Override
    public List<CartVo> getcartVos(Long id) {
        List<CartVo> list = (List<CartVo>) redisTemplate.boundHashOps("cartList").get(id);
        return list == null ? new ArrayList<>() : list;
    }

    //合并redis和cookie里面的数据
    @Override
    public List<CartVo> hebing(List<CartVo> cartVos, List<CartVo> cartVos1) {
        for (CartVo c : cartVos) {
            List<TbOrderItem> list = c.getCartDatailList();
            for (TbOrderItem t : list) {
                cartVos1 = addaddcartList(cartVos1, t.getItemId(), t.getNum());
            }
        }
        return cartVos1;
    }

    //根据商品id,查看商家明细
    private TbOrderItem getorderItem(List<TbOrderItem> list, Long itemId) {
        for (TbOrderItem tbOrderItem : list) {
            if (Objects.equals(tbOrderItem.getItemId(), itemId)) {
                return tbOrderItem;
            }
        }
        return null;
    }
}